var MathService = /** @class */ (function () {
    function MathService() {
    }
    /**
     * 获取小数点后的位数
     * @param value  number
     */
    MathService.prototype.getDigs = function (value) {
        var num = String(value);
        var index = num.indexOf('.');
        if (index === -1) {
            return 0;
        }
        else {
            return num.length - 1 - index;
        }
    };
    /**
     * 取值到小数位（四舍五入）
     * @param num number数值
     * @param digs (可选)保留的小数位数
     */
    MathService.prototype.toFixed = function (num, digs) {
        if (isNaN(digs) || !isFinite(digs)) {
            return String(num);
        }
        var times = Math.pow(10, digs);
        var des = String(num * times + 0.5);
        des = parseInt(des, 10) / times + '';
        return des;
    };
    /**
     * Math 加法
     * @param num1 number
     * @param num2 number
     * @param digs (可选)保留的小数位
     */
    MathService.prototype.add = function (num1, num2, digs) {
        var maxDigs = Math.max(this.getDigs(num1), this.getDigs(num2));
        var times = Math.pow(10, maxDigs);
        var value = (num1 * times + num2 * times) / times;
        return this.toFixed(value, digs);
    };
    /**
     * Math 减法
     * @param num1 number
     * @param num2 number
     * @param digs (可选)保留的小数位
     */
    MathService.prototype.subtract = function (num1, num2, digs) {
        var maxDigs = Math.max(this.getDigs(num1), this.getDigs(num2));
        var times = Math.pow(10, maxDigs);
        var value = (num1 * times - num2 * times) / times;
        return this.toFixed(value, digs);
    };
    /**
     * Math 乘法
     * @param num1 number
     * @param num2 number
     * @param digs (可选)保留的小数位
     */
    MathService.prototype.multiply = function (num1, num2, digs) {
        var maxDigs = Math.max(this.getDigs(num1), this.getDigs(num2));
        var times = Math.pow(10, maxDigs);
        var value = (num1 * times * num2 * times) / times;
        return this.toFixed(value, digs);
    };
    /**
     * Math 除法
     * @param num1 number
     * @param num2 number
     * @param digs (可选)保留的小数位
     */
    MathService.prototype.divide = function (num1, num2, digs) {
        // 分母不能为0
        if (num2 === 0) {
            return 0 + '';
        }
        var maxDigs = Math.max(this.getDigs(num1), this.getDigs(num2));
        var times = Math.pow(10, maxDigs);
        var value = (num1 * times / (num2 * times)) / times;
        return this.toFixed(value, digs);
    };
    return MathService;
}());
